package com.lonely.fx.tree_view;

import com.lonely.fx.tree_view.entity.TestCheckBoxTreeCellDto;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.control.cell.CheckBoxTreeCell;
import javafx.scene.control.cell.ChoiceBoxTreeCell;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;

/**
 * @date: 2022-08-04 10:19
 * @author: ztkj-hzb
 * @description: treeview 的 cellfactory 使用 ChoiceBoxTreeCell 效果演示,实现checkbox与数据联动
 **/
public class TreeViewCellFactoryWithCheckBoxTreeCellSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        AnchorPane anchorPane = new AnchorPane();

        //创建TreeView
        TreeView<TestCheckBoxTreeCellDto> treeView = new TreeView<>();

        //准备数据

        //创建根节点
        TreeItem<TestCheckBoxTreeCellDto> root = new TreeItem<>(new TestCheckBoxTreeCellDto(true, "中国"));

        TreeItem<TestCheckBoxTreeCellDto> hb = new TreeItem<>(new TestCheckBoxTreeCellDto(true, "湖北省"));
        TreeItem<TestCheckBoxTreeCellDto> hb_1 = new TreeItem<>(new TestCheckBoxTreeCellDto(true, "鄂州市"));
        TreeItem<TestCheckBoxTreeCellDto> hb_2 = new TreeItem<>(new TestCheckBoxTreeCellDto(true, "武汉市"));
        TreeItem<TestCheckBoxTreeCellDto> hb_3 = new TreeItem<>(new TestCheckBoxTreeCellDto(true, "襄樊市"));
        TreeItem<TestCheckBoxTreeCellDto> hb_4 = new TreeItem<>(new TestCheckBoxTreeCellDto(true, "黄冈市"));

        //添加子节点
        hb.getChildren().addAll(hb_1, hb_2, hb_3, hb_4);

        TreeItem<TestCheckBoxTreeCellDto> gd = new TreeItem<>(new TestCheckBoxTreeCellDto(false, "广东省"));
        TreeItem<TestCheckBoxTreeCellDto> gd_1 = new TreeItem<>(new TestCheckBoxTreeCellDto(false, "广州市"));
        TreeItem<TestCheckBoxTreeCellDto> gd_2 = new TreeItem<>(new TestCheckBoxTreeCellDto(false, "深圳市"));
        TreeItem<TestCheckBoxTreeCellDto> gd_3 = new TreeItem<>(new TestCheckBoxTreeCellDto(false, "东莞市"));
        gd.getChildren().addAll(gd_1, gd_2, gd_3);

        TreeItem<TestCheckBoxTreeCellDto> hn = new TreeItem<>(new TestCheckBoxTreeCellDto(false, "湖南省"));
        TreeItem<TestCheckBoxTreeCellDto> hn_1 = new TreeItem<>(new TestCheckBoxTreeCellDto(false, "长沙市"));
        TreeItem<TestCheckBoxTreeCellDto> hn_2 = new TreeItem<>(new TestCheckBoxTreeCellDto(true, "岳阳市"));
        hn.getChildren().addAll(hn_1, hn_2);

        root.getChildren().addAll(hb, gd, hn);

        //设置根节点
        treeView.setRoot(root);

        //设置宽高
        treeView.setPrefSize(300, 300);

        //设置是否展开子节点列表
        root.setExpanded(true);


        anchorPane.getChildren().addAll(treeView);
        AnchorPane.setTopAnchor(treeView, 100.0);
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();


        //设置编辑单元格样式
        treeView.setEditable(true);
        treeView.setCellFactory(CheckBoxTreeCell.forTreeView(param -> param.getValue().selectedProperty(), new StringConverter<TreeItem<TestCheckBoxTreeCellDto>>() {
            @Override
            public String toString(TreeItem<TestCheckBoxTreeCellDto> object) {
                return object.getValue().getData();
            }

            @Override
            public TreeItem<TestCheckBoxTreeCellDto> fromString(String string) {
                return null;
            }
        }));


        root.addEventHandler(TreeItem.treeNotificationEvent(), new EventHandler<TreeItem.TreeModificationEvent<Object>>() {
            @Override
            public void handle(TreeItem.TreeModificationEvent<Object> event) {
                System.out.println(event.getEventType());
            }
        });

    }
}
